package com.huawei.od;

import com.huawei.pojo.ListNodeT1;

/**
 * 有n个人围成一圈，顺序排号为1-n。从第一个人开始报数(从1到3报数)，凡报到3的人退出圈子，问最后留下的是原来第几号的那位。
 */
public class T1 {
    public static void main(String[] args) {
        ListNodeT1 loop = ListNodeT1.loop(6);
        int left = leftNum(loop, 3);
        System.out.println(left);
    }

    private static int leftNum(ListNodeT1 listNode, int i) {
        ListNodeT1 index = listNode;
        int count = 0;
        while (true) {
            if (index.getNext() == null) {
                return index.getNum();
            }
            count++;
            if(count == i-1){
                ListNodeT1 afterNext = index.getNext().getNext();
                if(index == afterNext){
                    index.setNext(null);
                    continue;
                }
                index.setNext(afterNext);
                count = 0;
            }
            index = index.getNext();
        }
    }
}
